﻿using DomainExample.DomainModel.PersonModel;
using DomainExample.Repository.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DomainExample.Repository.Tests.PersonRepositoryTests
{
    [TestClass]
    public sealed class PersonRepositoryPersistTests : PersonRepositoryTests
    {
        [TestInitialize]
        public void SetUp()
        {
            CreateSchema();
        }

        [TestCleanup]
        public void TearDown()
        {
            DestroySchema();
        }

        [TestMethod]
        [TestCategory("Database Integration")]
        public void PersonRepositoryPersistWithNewPersonExpectSaved()
        {
            const string expectedFirstName = "Jane";
            const string expectedLastName = "Example";

            var result = AddTestPerson(expectedFirstName, expectedLastName);

            Assert.AreNotEqual(0, result.Id.Value);
        }

        [TestMethod]
        [TestCategory("Database Integration")]
        public void PersonRepositoryPersistWithExistingPersonExpectSaved()
        {
            const string expectedFirstName = "John";
            const string expectedLastName = "Test";

            var testPerson = AddTestPerson("x", "x");

            testPerson.FirstName = expectedFirstName;
            testPerson.LastName = expectedLastName;

            var result = Target.Persist(testPerson);

            Assert.AreEqual(testPerson.Id, result.Id);
            Assert.AreEqual(expectedFirstName, result.FirstName);
            Assert.AreEqual(expectedLastName, result.LastName);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidEntityException))]
        public void PersonRepositoryPersistWithInvalidPersonExpectException()
        {
            var testPerson = Person.NewPerson();

            Target.Persist(testPerson);
        }
    }
}
